<style>
td.gennam {text-align:left}
td.genval {text-align:right}
td.txsval {text-align:right}
td.netcnt {text-align:right}
td.netbts {text-align:right}
div.tablab {
	font-weight:bold;
	margin-bottom:10px;
}
</style>
<div id="nocounters_message" style="display:none">
	<br>
	Counters disabled in the config file (<code>Stats.NoCounters</code>)</div>
<table width="100%" id="counters_table" style="display:none" border="0">
<tr>
<td valign="top" align="left" colspan="2">
	<span onclick="cnts_combine_groups.click()" class="hand" title="Combine similar counters">
	<input type="checkbox" id="cnts_combine_groups" onchange="type_changed()"
		onclick="event.stopPropagation()"> Combine
	</span>
</td>
<td valign="top" align="right" colspan="3">
	<span onclick="cnts_type_gen.click()" class="hand" title="Show Generic counters">
	<input type="radio" name="cnts_type" id="cnts_type_gen" onchange="type_changed()" checked
		onclick="event.stopPropagation()"> Generic
	</span>
	<span onclick="cnts_type_net.click()" class="hand" title="Show Network counters">
	<input type="radio" name="cnts_type" id="cnts_type_net" onchange="type_changed()"
		onclick="event.stopPropagation()"> Network
	</span>
</tr>
</tr>

<tr id="gen_stats_row" style="display:table-row">
<td valign="top" align="center">
	<div class="tablab">Generic counters A</div>
	<table class="mono bord" width="100%" id="tab_gen_a">
	<tr>
		<th>Counter
		<th>Value
	</tr>
	</table>
</td>
<td valign="top" align="center">
	<div class="tablab">Generic counters B</div>
	<table class="mono bord" width="100%" id="tab_gen_b">
	<tr>
		<th>Counter
		<th>Value
	</tr>
	</table>
</td>
<td valign="top" align="center">
	<div class="tablab">Tx generic</div>
	<table class="mono bord" width="100%" id="tab_txs_a">
	<tr>
		<th>Counter
		<th>Value
	</tr>
	</table>
</td>
<td valign="top" align="center">
	<div class="tablab">Tx sorting</div>
	<table class="mono bord" width="100%" id="tab_txs_b">
	<tr>
		<th>Counter
		<th>Value
	</tr>
	</table>
</td>
</tr>

<tr id="net_stats_row" style="display:none">
<td valign="top" align="center" colspan="5">
	<div class="tablab">Network commands</div>
	<table class="mono bord" id="tab_net">
	<colset>
		<col width="110">
		<col width="90">
		<col width="120">
		<col width="90">
		<col width="120">
	</colset>
	<tr>
		<th rowspan="2">Network Message
		<th colspan="2">Message Received
		<th colspan="2">Message Sent
	</tr>
	<tr>
		<th>Times
		<th>Bytes
		<th>Times
		<th>Bytes
	</tr>
    </table>
</td>
</tr>
</table>
<script>
function type_changed() {
	if (cnts_type_gen.checked) {
		net_stats_row.style.display = 'none'
		gen_stats_row.style.display = 'table-row'
		localStorage.setItem("counters_view_type", "gen")

		cnts_combine_groups.disabled = false
		localStorage.setItem("counters_combine_groups", cnts_combine_groups.checked)
		css('tr.cnt_combined', 'display', cnts_combine_groups.checked?'table-row':'none')
		css('tr.cnt_expanded', 'display', cnts_combine_groups.checked?'none':'table-row')
	} else {
		net_stats_row.style.display = 'table-row'
		gen_stats_row.style.display = 'none'
		localStorage.setItem("counters_view_type", "net")
		cnts_combine_groups.disabled = true
	}

}

function group_only(s) {
	var dashpos = s.indexOf("-")
	if (dashpos > 0) {
		return s.substr(0, dashpos+1)
	}
	return s
}

function make_clickable(el, prefix='') {
	var s = el.innerText
	el.title = 'Click to Copy'
	el.classList.add('hand')
	el["text2copy"] = prefix+group_only(s)
	el.addEventListener("click", copyonclick)
}

function do_one_table(output_table, data, prefix='') {
	for (i=0; i<data.length; i++) {
		var row
		if (i < output_table.rows.length-1) {
			row = output_table.rows[i+1]
			row.className = ''
		} else {
			row = output_table.insertRow(-1)
			row.insertCell(-1)
			row.insertCell(-1)
			row.cells[0].className = 'gennam'
			row.cells[1].className = 'genval'
		}
		row.cells[0].innerText = data[i][0]
		row.cells[1].innerText = data[i][1].toLocaleString()
		if (data[i][2]==1) {
			row.classList.add('cnt_expanded')
		} else if (data[i][2]==2) {
			row.classList.add('cnt_combined')
		}
		make_clickable(row.cells[0], prefix)
	}
	while (output_table.rows.length > data.length + 1) {
		output_table.deleteRow(output_table.rows.length-1)
	}
}


function prepare_arrays(cnts, cback_fn) {
	if (cnts==null) return
	var lgr_val = ''
	var lgr_cnt, lgr_arr

	for (i=0; i<cnts.length; i++) {
		var arr = cback_fn(cnts[i].var)
		var gr_val = group_only(cnts[i].var)
		var group = gr_val != cnts[i].var

		if (group) {
			if (gr_val==lgr_val) {
				lgr_cnt += cnts[i].cnt
			} else {
				if (lgr_val!='')  lgr_arr.push([lgr_val+"*", lgr_cnt, 2])
				lgr_val = gr_val
				lgr_arr = arr
				lgr_cnt = cnts[i].cnt
			}
			arr.push([cnts[i].var, cnts[i].cnt, 1])
		} else {
			// non-groupable counter
			if (lgr_val!='') {
				lgr_arr.push([lgr_val+"*", lgr_cnt, 2])
				lgr_val = ''
			}
			arr.push([cnts[i].var, cnts[i].cnt, 0])
		}
	}
	if (lgr_val!='') {
		lgr_arr.push([lgr_val+"*", lgr_cnt, 2])
	}
}


function ref_counters() {
	var aj = ajax()
	aj.onerror=function() {
		setTimeout(ref_counters, 10000)
	}
	aj.onload=function() {
		try {
			var i, cts = JSON.parse(aj.responseText)
			//console.log(cts)

			if (cts.gen==null && cts.txs==null && cts.net==null) {
				nocounters_message.style.display='block'
				counters_table.style.display='none'
				return
			}

			var gen_a = new Array()
			var gen_b = new Array()
			function bc_gen(val) {
				return (val[0]>='M' || val.substr(0,3)=='Ban') ? gen_b : gen_a
			}
			prepare_arrays(cts.gen, bc_gen)
			do_one_table(tab_gen_a, gen_a)
			do_one_table(tab_gen_b, gen_b)

			var txs_a = new Array()
			var txs_b = new Array()
			function bc_txs(val) {
				var prefix = val.substr(0,4)
				if (prefix=='Pkgs' || prefix=='Sort')  return txs_b
				return txs_a
			}
			prepare_arrays(cts.txs, bc_txs)
			do_one_table(tab_txs_a, txs_a, "Tx")
			do_one_table(tab_txs_b, txs_b, "Tx")


			if (cts.net!=null) {
				for (i=0; i<cts.net.length; i++) {
					var row
					if (i < tab_net.rows.length-2) {
						row = tab_net.rows[i+2]
					} else {
						row = tab_net.insertRow(-1)
						row.insertCell(-1)
						row.insertCell(-1)
						row.insertCell(-1)
						row.insertCell(-1)
						row.insertCell(-1)
						row.cells[0].className = 'netnam'
						row.cells[1].className = 'netcnt'
						row.cells[2].className = 'netbts'
						row.cells[3].className = 'netcnt'
						row.cells[4].className = 'netbts'
					}
					row.cells[0].innerText = cts.net[i].var
					make_clickable(row.cells[0])
					row.cells[1].innerText = cts.net[i].rcvd.toLocaleString()
					row.cells[2].innerText = cts.net[i].rbts.toLocaleString()
					row.cells[3].innerText = cts.net[i].sent.toLocaleString()
					row.cells[4].innerText = cts.net[i].sbts.toLocaleString()
				}
			}
			while (tab_net.rows.length > cts.net.length + 2) {
				tab_net.deleteRow(tab_net.rows.length-2)
			}
			nocounters_message.style.display='none'
			counters_table.style.display='table'
		} catch(e) {
			console.log("error", e)
		}
		setTimeout(ref_counters, 1000)
	}
	aj.open("GET","counts.json",true)
	aj.send(null)
}
ref_counters()

function load_settings() {
	if (localStorage.getItem("counters_view_type") == "net")  cnts_type_net.click()
	cnts_combine_groups.checked = localStorage.getItem("counters_combine_groups") !== 'false'
	type_changed()
}
load_settings()

</script>